
def update_feeds(sender, instance, signal, created, *args, **kwargs):  
    """Update feeds when rep is created."""
    # requires listener to be setup in models, like so.
    # dispatcher.connect(update_feedmap, signal=signals.post_save, sender=Rep)
    from aggregator.models import Feed
    from chicago.models import FeedMap, Rep

    #if 'created' in kwargs:  
    #    if kwargs['created']:  
    if created:
        try:
            try:
                feed = Feed.objects.get(feed_url=instance.google_feed_url())
            except Feed.DoesNotExist:
                feed = Feed(
                    title = 'News on %s' % instance,
                    public_url = instance.google_public_url(),
                    feed_url = instance.google_feed_url(),
                    is_defunct = False)
                feed.save()

            feedmap = FeedMap(
                    feed = feed,
                    object = instance)
            feedmap.save()
        except:
            # If something fails just continue... we should probably print a
            # message like "you need to create the feed manually" but oh well.
            pass


def del_feeds(sender, instance, signal, **kwargs):

    #delete feedmap and related objects

    for f in instance.feeds.all():
        #delete feed and feedmap entries
        for i in feed.feeditem_set.all():
            i.delete()
        f.feed.delete()
        f.delete()
